﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Skin2D.Bone;
using System.ComponentModel;
using System.Globalization;
using System.Drawing;
using System.ComponentModel.Design.Serialization;
using System.Reflection;

namespace Skin2D.Control
{

    public class PointFConverter : TypeConverter
    {
        // Overrides the CanConvertFrom method of TypeConverter.
        // The ITypeDescriptorContext interface provides the context for the
        // conversion. Typically, this interface is used at design time to 
        // provide information about the design-time container.
        public override bool CanConvertFrom(ITypeDescriptorContext context,
           Type sourceType)
        {

            if (sourceType == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }
        // Overrides the ConvertFrom method of TypeConverter.
        public override object ConvertFrom(ITypeDescriptorContext context,
           CultureInfo culture, object value)
        {
            try
            {
                if (value is string)
                {
                    string[] v = ((string)value).Split(new char[] { ',' });
                    return new PointF(int.Parse(v[0]), int.Parse(v[1]));
                }
            }
            catch (Exception ex)
            {
            }
            return base.ConvertFrom(context, culture, value);
        }

        // Overrides the ConvertTo method of TypeConverter.
        public override object ConvertTo(ITypeDescriptorContext context,
           CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return ((PointF)value).X + "," + ((PointF)value).Y;
            }
            if (destinationType == typeof(InstanceDescriptor))
            {
                 PointF pt = (PointF)value;

                 ConstructorInfo ctor = typeof(Point).GetConstructor(new Type[] {typeof(float), typeof(float)});
                 if (ctor != null)
                     return new InstanceDescriptor(ctor, new object[] { pt.X, pt.Y });
 
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            PropertyDescriptorCollection tmp = TypeDescriptor.GetProperties(value);
            PropertyDescriptorCollection collect = new PropertyDescriptorCollection(new PropertyDescriptor[] { tmp[1], tmp[2] });
            return collect;
        }
    }

    class CtrPropertyGrid
    {

        public CtrPropertyGrid(Panel panel)
        {
            m_container = panel;
            Init();
        }

        private void Init()
        {
            m_proGrid = new PropertyGrid();
            m_proGrid.Dock = DockStyle.Fill;
            m_proGrid.Name = "CtrPropertyGrid";

            m_container.Controls.Clear();
            m_container.Controls.Add(m_proGrid);
        }

        public void SetData(Bone2D bone)
        {
            m_proGrid.SelectedObject = bone;
        }

        private Bone2D m_data;
        private Panel m_container;
        private PropertyGrid m_proGrid;
    }
}
